//
//  TTILanguageTool.m
//  DanoneEA
//
//  Created by Senela on 16/12/26.
//  Copyright © 2016年 翔傲科技. All rights reserved.
//
 


#define LANGUAGE_SET @"langeuageset"

#import "AppDelegate.h"
#import "TTILanguageTool.h"

#import "DNLeftDrawerTableVC.h"

static TTILanguageTool *sharedModel;

@interface TTILanguageTool()

@property(nonatomic,strong)NSBundle *bundle;
@property(nonatomic,copy)NSString *language;

@end

@implementation TTILanguageTool
+(id)sharedInstance
{
    if (!sharedModel)
    {
        sharedModel = [[TTILanguageTool alloc]init];
    }
    
    return sharedModel;
}

-(instancetype)init
{
    self = [super init];
    if (self)
    {
        [self initLanguage];
    }
    
    return self;
}

-(void)initLanguage
{
    NSString *tmp = [[NSUserDefaults standardUserDefaults]objectForKey:LANGUAGE_SET];
    NSString *path;
    //默认是英文
    if (!tmp)
    {
        tmp = English;
    }
    
    self.language = tmp;
    path = [[NSBundle mainBundle]pathForResource:self.language ofType:@"lproj"];
    self.bundle = [NSBundle bundleWithPath:path];
}

-(NSString *)getStringForKey:(NSString *)key withTable:(NSString *)table
{
    if (self.bundle)
    {
        return NSLocalizedStringFromTableInBundle(key, table, self.bundle, @"");
    }
    
    return NSLocalizedStringFromTable(key, table, @"");
}

 

-(void)setNewLanguage:(NSString *)language
{
    if ([language isEqualToString:self.language])
    {
        return;
    }
    
    if ([language isEqualToString:English] || [language isEqualToString:Chinese]
        || [language isEqualToString:Malay] || [language isEqualToString:Thai]
        || [language isEqualToString:Turkish] || [language isEqualToString:Polish]
        || [language isEqualToString:German])
    {
        NSString *path = [[NSBundle mainBundle]pathForResource:language ofType:@"lproj"];
        self.bundle = [NSBundle bundleWithPath:path];
    } 
        
    self.language = language;
    [[NSUserDefaults standardUserDefaults]setObject:language forKey:LANGUAGE_SET];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [self resetRootViewController];
}

//重新设置
-(void)resetRootViewController
{
    appDelegate.tabBarVC = nil;
    appDelegate.tabBarVC = [[DNTabBarViewController alloc] init];
    
    UIViewController * leftSideDrawerViewController = [[DNLeftDrawerTableVC alloc] init];
    UINavigationController * leftSideNavController = [[UINavigationController alloc] initWithRootViewController:leftSideDrawerViewController];
    [leftSideNavController setRestorationIdentifier:@"DNLeftNavigationControllerRestorationKey"];
    
    // add right side drawer view controller when current view will appear
    UIViewController * rightSideDrawerViewController = [[DNLeftDrawerTableVC alloc] init];
    UINavigationController * rightSideNavController = [[UINavigationController alloc] initWithRootViewController:rightSideDrawerViewController];
    [rightSideNavController setRestorationIdentifier:@"DNRightNavigationControllerRestorationKey"];
    
    
    appDelegate.drawerController = nil;
    appDelegate.drawerController = [[MMDrawerController alloc]
                         initWithCenterViewController:appDelegate.tabBarVC
                         leftDrawerViewController:leftSideNavController
                         rightDrawerViewController:rightSideNavController];
    [appDelegate.drawerController setShowsShadow:NO];
    [appDelegate.drawerController setRestorationIdentifier:@"MMDrawer"];
    [appDelegate.drawerController setMaximumRightDrawerWidth:240.0];
    [appDelegate.drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
    [appDelegate.drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    [appDelegate.window setRootViewController:appDelegate.drawerController];
}

@end
